package com.peng.stack_queue.linkedlist;

public class LinkedListStackOrQueue {


    static class Node {
        public int val;
        public Node last;
        public Node next;

        public Node(int val) {
            this.val = val;
        }
    }

    static class DoubleEndsQueue {

        private Node tail;
        private Node head;

        public void addFromHead(int val) {
            Node node = new Node(val);
            if (head == null) {
                head = node;
                tail = node;
            } else {
                node.next = head;
                head.last = node;
                head = node;
            }
        }

        public void addFromBottom(int val) {
            Node node = new Node(val);
            if (head == null) {
                head = node;
                tail = node;
            } else {
                tail.next = node;
                node.last = tail;
                tail = node;
            }
        }

        public Integer popFromHead() {
            if (head == null) {
                return null;
            }
            Node popNode = head;

            if (head == tail) {
                head = null;
                tail = null;
            } else {
                head = head.next;

                head.last = null;

                popNode.next = null;
            }


            return popNode.val;
        }

        public Integer popFromBottom() {
            if (head == null) {
                return null;
            }
            Node popNode = tail;
            if (head == tail) {
                head = null;
                tail = null;
            } else {
                tail = tail.last;
                tail.next = null;

                popNode.last = null;
            }
            return popNode.val;
        }

        public void print() {
            Node cur = head;
            while (cur != null) {
                System.out.println(cur.val);
                cur = cur.next;
            }
        }

    }


    public static void main(String[] args) {
        DoubleEndsQueue doubleEndsQueue = new DoubleEndsQueue();
        doubleEndsQueue.addFromHead(1);
        doubleEndsQueue.addFromHead(2);
        doubleEndsQueue.addFromHead(3);
        doubleEndsQueue.addFromHead(4);
        doubleEndsQueue.addFromHead(5);
        /**
         * 从头部放，从头部出，就是栈
          */
//        int num = doubleEndsQueue.popFromHead();
//        System.out.println(num);
//        num = doubleEndsQueue.popFromHead();
//        System.out.println(num);
//        num = doubleEndsQueue.popFromHead();
//        System.out.println(num);
//        num = doubleEndsQueue.popFromHead();
//        System.out.println(num);
        /**
         * 从头部放，从尾部出，就是队列
         */
        int num = doubleEndsQueue.popFromBottom();
        System.out.println(num);
        num = doubleEndsQueue.popFromBottom();
        System.out.println(num);
        num = doubleEndsQueue.popFromBottom();
        System.out.println(num);
        num = doubleEndsQueue.popFromBottom();
        System.out.println(num);

    }
}
